import 'package:flutter/material.dart';

/// 页面跳转
class IntentApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primarySwatch: Colors.blue),
      home: HomeApp(),
      routes: <String, WidgetBuilder>{
        '/a': (BuildContext context) => MyPage(
              title: 'Page A',
            ),
        '/b': (BuildContext context) => MyPage(
              title: 'Page B',
            ),
        '/c': (BuildContext context) => MyPage(
              title: 'Page C',
            ),
      },
    );
  }
}

/// 主页
class HomeApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => HomeAppState();
}

class HomeAppState extends State<HomeApp> {
  // 跳转页面，必须在MaterialApp的routes中注册
  _navigator(BuildContext context) {
    // 类似于StartActivity
    Navigator.of(context).pushNamed('/b');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('主页'),
      ),
      body: Center(
        child: MaterialButton(
          onPressed: () {
            _navigator(context);
          },
          child: Text('跳转'),
        ),
      ),
    );
  }
}

class MyPage extends StatelessWidget {
  final String title;

  MyPage({Key key, this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(this.title),
      ),
      body: Center(
        child: Text(this.title),
      ),
    );
  }
}
